using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectQuanHuyen()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_QUANHUYEN
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectQuanHuyenPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_QUANHUYEN,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListQuanHuyen", CreateViewModel(data));
		}

		public ActionResult SelectQuanHuyenByTinhThanh(int idtinhthanh, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDTinhThanh = idtinhthanh,
				EnumViewModelType = EnumViewModel.ADMIN_QUANHUYEN,
				CurrentPage = page
			};
			return View("SelectQuanHuyen", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteQuanHuyen(int id)
		{
			return Json(new{
				Success = _repQuanHuyen.Delete(id),
				RecordCount = _repQuanHuyen.SelectAll().Count,
				DeleteId = id
			});
		}

        public ActionResult EditQuanHuyen(int page, int id, int idtinhthanh)
		{
			var data = new DataTransferViewModel
			{
                IDTinhThanh = idtinhthanh,
				EnumViewModelType = EnumViewModel.ADMIN_QUANHUYEN,
				CurrentPage = page,
				EditID = id
			};
			return PartialView("Templates/TH_EditQuanHuyen", CreateViewModel(data));
		}

        public ActionResult CancelEditingQuanHuyen(int page, int idtinhthanh)
		{
			var data = new DataTransferViewModel
			{
                IDTinhThanh = idtinhthanh,
				EnumViewModelType = EnumViewModel.ADMIN_QUANHUYEN,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListQuanHuyen", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateQuanHuyen(FormCollection forms)
		{
			string id = forms.GetValues("QuanHuyen_ID")[0];
			string idtinhthanh = forms.GetValues("QuanHuyen_IDTinhThanh")[0];
			string tenquanhuyen = forms.GetValues("QuanHuyen_TenQuanHuyen")[0];
			string currentpage = forms.GetValues("QuanHuyen_CurrentPage")[0];
			var quanhuyen = _repQuanHuyen.SelectByID(int.Parse(id));
			quanhuyen.IDTinhThanh = int.Parse(idtinhthanh);
			quanhuyen.TenQuanHuyen = tenquanhuyen;
			var result = _repQuanHuyen.Save();
			var data = new DataTransferViewModel
			{
                IDTinhThanh = int.Parse(idtinhthanh),
				EnumViewModelType = EnumViewModel.ADMIN_QUANHUYEN,
				CurrentPage = int.Parse(currentpage)
			};
			return PartialView("Templates/TH_ListQuanHuyen", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertQuanHuyen(FormCollection forms)
		{
			string idtinhthanh = forms.GetValues("QuanHuyen_IDTinhThanh")[0];
			string tenquanhuyen = forms.GetValues("QuanHuyen_TenQuanHuyen")[0];
			var newItem = new QuanHuyen
			{
				IDTinhThanh = int.Parse(idtinhthanh),
				TenQuanHuyen = tenquanhuyen
			};
			var result = _repQuanHuyen.Insert(newItem);
			var data = new DataTransferViewModel
			{
                IDTinhThanh = int.Parse(idtinhthanh),
				EnumViewModelType = EnumViewModel.ADMIN_QUANHUYEN,
				Added = result
			};
			return Json(new{
				Success = result,
				Message = "A new item has been added!",
				PartialViewHtml = RenderPartialViewToString("Templates/TH_ListQuanHuyen", CreateViewModel(data))
			});
		}


	}
}